package com.xihuanjun.sdk.service;

import com.xihuanjun.sdk.dao.SapDeliveryNoteDao;
import com.xihuanjun.sdk.dao.SapItemDao;
import com.xihuanjun.sdk.dao.SapOrderDao;
import com.xihuanjun.sdk.entity.SapItem;
import com.xihuanjun.sdk.entity.SapOrder;
import com.xihuanjun.sdk.entity.SapOrderItem;
import com.xihuanjun.sdk.entity.SapOrderItemBatch;
import com.xihuanjun.sdk.enums.YNEnum;
import com.xihuanjun.sdk.exception.SapException;
import com.xihuanjun.sdk.util.BatchUtils;
import com.xihuanjun.sdk.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Copyright (C), 2024-09-03
 * FileName: OrderService
 * Author:   溪浣君
 * Date:     2024-09-03 16:20
 * Description: 销售订单业务
 */
@Service
@Slf4j
public class OrderService {

    @Autowired
    SapOrderDao orderDao;

    @Autowired
    SapDeliveryNoteDao deliveryNoteDao;

    @Autowired
    SapItemDao sapItemDao;

    @Autowired(required = false)
    IItemService itemService;

    /**
     * 根据一个销售订单号自动进行交货处理
     * @param docEntry 订单编号
     * @param batchNumberService 获取批次信息的实现
     * @param clazz
     * @param whsCode 交货仓库，没有则用订单行里的仓库
     */
    public <T extends SapOrder> long toDelivery(long docEntry, IBatchNumberService batchNumberService,Class<T> clazz,String whsCode){
        T order = orderDao.getObjectByPrimaryKey(docEntry + "", clazz);
        List<SapOrderItem> list=new ArrayList<>();
        BatchUtils batchUtils=new BatchUtils(batchNumberService);
        for (Object line : order.getDocumentLines()) {
            SapOrderItem item=(SapOrderItem) line;
            SapOrderItem itemNew=new SapOrderItem();
            //设置绑定关系
            itemNew.setBaseEntry(order.getDocEntry());
            itemNew.setBaseType("17");
            itemNew.setBaseLine(item.getLineNum());
            //设置批次
//            SapItem sapItem = sapItemDao.getObjectByPrimaryKey(item.getItemCode(), SapItem.class);
//            if(sapItem.getInventoryItem()== YNEnum.tYES){//是仓库物料，不是虚拟物料，才需要选择批次
            if(itemService==null){
                throw new SapException("IoC容器里没有IItemService的实现实例");
            }
            if(itemService.isInventoryItem(item.getItemCode())){
                List<SapOrderItemBatch> itemBatchNumbers =batchUtils.getBatchs(item.getItemCode(), item.getQuantity(),StringUtils.hasText(whsCode) ? whsCode : item.getWarehouseCode());
                itemNew.setBatchNumbers(itemBatchNumbers);
            }
            list.add(itemNew);
        }
        T t;
        try {
            t = clazz.newInstance();
            t.setDocumentLines(list);
            t.setCardCode(order.getCardCode());
            t.setDocDate(order.getDocDate());
            t.setDocDueDate(order.getDocDueDate());
            System.out.println(JsonUtils.object2Json(t));
        } catch (Exception e) {
            throw new SapException("实例化T错误："+e.getMessage());
        }
        String s = deliveryNoteDao.addObject(t);
        return Long.parseLong(s);
    }
}
